package com.chc.web.role.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chc.utils.CommonResult;
import com.chc.web.role.entity.*;
import com.chc.web.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/04/19/17:51
 * @Description:
 */

@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询角色列表
     * */
    @GetMapping("/list")
    public CommonResult list(RoleParm parm){
        IPage<Role> list = roleService.list(parm);
        return CommonResult.success("查询成功!",list);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping
    public CommonResult addRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        if(save){
            return CommonResult.success("新增角色成功");
        }
        return CommonResult.error("新增角色失败");
    }

    /**
     * 编辑角色
     * @param role
     * @return
     */
    @PutMapping
    public CommonResult editRole(@RequestBody Role role){
        boolean save = roleService.updateById(role);
        if(save){
            return CommonResult.success("编辑角色成功");
        }
        return CommonResult.error("编辑角色失败");
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/{roleId}")
    public CommonResult deleteRole(@PathVariable("roleId") Long roleId){
        boolean save = roleService.removeById(roleId);
        if(save){
            return CommonResult.success("删除角色成功");
        }
        return CommonResult.error("删除角色失败");
    }

    //查询分配权限树回显
    @GetMapping("/getAssignTree")
    public CommonResult getAssignMenuTree(RoleAssignParm roleParm){
        RolePermissionVo assignTree = roleService.getAssignTree(roleParm);
        return CommonResult.success("查询成功",assignTree);
    }
    //分配权限保存
    @PostMapping("/saveAssignRole")
    public CommonResult saveAssignRole(@RequestBody RolePermissionParm parm){
        roleService.saveAssignRole(parm.getRoleId(),parm.getList());
        return CommonResult.success("分配权限成功!");
    }
}
